package com.lyr.tranfimg.service.impl;


import com.lyr.tranfimg.service.TransferImgService;
import com.lyr.tranfimg.util.MyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;


/**
 * @author liyiruo
 * @Description
 * @Date 2024/10/29 17:10
 */
@Service
public class TransferImgServiceImpl implements TransferImgService {
    private static final Logger logger = LoggerFactory.getLogger(TransferImgServiceImpl.class);

    @Value("${transfer.source.path}")
    private String sourcePath;
    @Value("${transfer.target.path}")
    private String targetPath;
    //为什么从配置文件取不到
//    @Value("{transfer.target.fixFileName}")
    private  String fixFileName="1.jpg";
//    @Value("{transfer.filename.suffix}")
    private String filenameSuffix=".jpg";
    @Value("${transfer.delay}")
    private Long delay;


    @Override
    public void findAndCopyAndDelete(String id) {
        //拼接文件名称
        String fileName = id.concat(filenameSuffix);
        //源文件夹，照片放在的位置
        String rootPath = sourcePath;
        //在目录下查找到的文件完整路径
        String fileFullPath = MyUtil.findFileFullPath(rootPath, fileName);
        //文件要保存的位置
        String dest = targetPath;
        //要保存的文件名
        String destFileName = fixFileName;
        //文件要保存的位置+名称
        String destFullPath = pingJiePath(dest, destFileName);
        if (fileFullPath != null) {
            boolean copyOk = MyUtil.copy(fileFullPath, destFullPath);
            if (copyOk) {
                MyUtil.scheduleFileDeletion(new File(destFullPath), delay);
            }
        }
    }


    private String pingJiePath(String path, String filename) {
        String fullPath = "";
        if (!path.endsWith(File.separator)) {
            path = path.concat(File.separator);
        }
        fullPath = path.concat(filename);
        return fullPath;
    }

}
